package net.sahv.bdyz.controller.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import net.sahv.bdyz.model.Fabric;
import net.sahv.bdyz.model.FabricFilter;
import net.sahv.bdyz.model.FabricMorepic;
import net.sahv.bdyz.model.FabricPic;
import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.model.MemberAttention;
import net.sahv.bdyz.service.FabricFilterService;
import net.sahv.bdyz.service.FabricMorepicService;
import net.sahv.bdyz.service.FabricPicService;
import net.sahv.bdyz.service.FabricService;
import net.sahv.bdyz.service.LanguageService;
import net.sahv.bdyz.service.MemberAttentionService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.util.PageInfo;

/**
 * @author 作者 : 任凯伟
 * @date 创建时间：2016年8月4日 下午3:26:38
 * @version 1.0
 * @see 面料
 */
@Controller
@RequestMapping("web/fabric")
public class FabricController {
	@Autowired
	private FabricFilterService fabricFilterService;// 类型参数
	@Autowired
	private FabricService fabricService;// 面料
	@Autowired
	private FabricPicService fabricPicService;// 面料图片
	@Autowired
	private MemberService memberService;// 会员
	@Autowired
	private LanguageService languageService;// 语言
	@Autowired
	private FabricMorepicService fabricMorepicService;// 颜色
	@Autowired
	private MemberAttentionService memberAttentionService;// 会员关注信息

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月4日 下午4:20:22
	 * @version 1.0
	 * @see 条件查询
	 * @param type
	 *            类别 （catalog 面料目录 cloth 版布 sample 样本）query 条件 sId 状态（1：现货 2：订货
	 *            3：最新 4：最受关注 5：价格升序 6：价格降序）
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping("/{type}/{query}")
	public String query(@PathVariable("query") String query, @PathVariable("type") String type, Integer sId,
			PageInfo pageInfo, Model model,HttpServletRequest request,HttpServletResponse response) {
		String language = languageService.getCurrentLanguage();
		
		if (pageInfo == null) {
			pageInfo = new PageInfo();
		}
		pageInfo.setPageSize(15);
		if (pageInfo.getPage() < 2 || pageInfo.getPage() >= pageInfo.getTotalPage()) {
			pageInfo.setCount(0);
		}
		String tValue = "";
		if ("catalog".equals(type)) {
			tValue = "1";
		} else if ("cloth".equals(type)) {
			tValue = "2";
		} else {
			tValue = "3";
		}
		String where = " F_IS_DELETE=0 and F_SEND_STATE=1 and F_TYPE="
				+ tValue;
		String orderStr = " order by rand() ";
		if (query != null) {
			String[] idList = query.split("-");
			if (idList.length > 0) {
				switch (idList.length) {
				case 1:// 一个条件
					if (!"all".equals(idList[0])) {
						where += " and (F_INGREDIENTS=" + idList[0] + " or F_WEAVING=" + idList[0] + " or F_G="
								+ idList[0] + " or F_SPECIES=" + idList[0] + ")";
					}
					break;
				case 2:// 两个查询条件
					where += " and (F_INGREDIENTS=" + idList[0] + " or F_WEAVING=" + idList[0] + " or F_G=" + idList[0]
							+ " or F_SPECIES=" + idList[0] + ") and (F_INGREDIENTS=" + idList[1] + " or F_WEAVING="
							+ idList[1] + " or F_G=" + idList[1] + " or F_SPECIES=" + idList[1] + ")";
					break;
				case 3:// 三个查询条件
					where += " and (F_INGREDIENTS=" + idList[0] + " or F_WEAVING=" + idList[0] + " or F_G=" + idList[0]
							+ " or F_SPECIES=" + idList[0] + ") and (F_INGREDIENTS=" + idList[1] + " or F_WEAVING="
							+ idList[1] + " or F_G=" + idList[1] + " or F_SPECIES=" + idList[1]
							+ ") and (F_INGREDIENTS=" + idList[2] + " or F_WEAVING=" + idList[2] + " or F_G="
							+ idList[2] + " or F_SPECIES=" + idList[2] + ")";
					break;
				case 4:// 4个查询条件
					where += " and (F_INGREDIENTS=" + idList[0] + " or F_WEAVING=" + idList[0] + " or F_G=" + idList[0]
							+ " or F_SPECIES=" + idList[0] + ") and (F_INGREDIENTS=" + idList[1] + " or F_WEAVING="
							+ idList[1] + " or F_G=" + idList[1] + " or F_SPECIES=" + idList[1]
							+ ") and (F_INGREDIENTS=" + idList[2] + " or F_WEAVING=" + idList[2] + " or F_G="
							+ idList[2] + " or F_SPECIES=" + idList[2] + ") and (F_INGREDIENTS=" + idList[3]
							+ " or F_WEAVING=" + idList[3] + " or F_G=" + idList[3] + " or F_SPECIES=" + idList[3]
							+ ")";
					break;
				default:
					break;
				}
			}
			if (sId != null) {
				switch (sId) {
				case 1:// 1：现货
					where += " and F_STATE=0";
					break;
				case 2:// 2：订货
					where += " and F_STATE=1";
					break;
				case 3:// 3：最新
					orderStr = " order by F_SEND_TIME desc";
					break;
				case 4:// 4：最受关注
					orderStr = " order by F_FOCUS_ON desc";
					break;
				case 5:// 5：价格升序
					orderStr = " order by F_PRICE asc";
					break;
				case 6:// 6：价格降序
					orderStr = " order by F_PRICE desc";
					break;
				case 7:// 7：印花底布
					where += " and F_STATE=2";
					break;
				default:
					break;
				}
			}
			Member member = memberService.getCurrent();// 获取当前登录会员
			int mId = 0;
			if (member != null) {
				mId = member.getmId();
			}
			List<Map<String, Object>> fList = fabricService.getByPage(pageInfo, where, orderStr, mId);
			List<FabricFilter> ffList = fabricFilterService.selectAll();// 面料参数
			if (fList.size() > 0) {
				pageInfo.setCount(Integer.parseInt(fList.get(0).get("myCount").toString()));
				pageInfo.setTotalPage((int) Math
						.ceil(Integer.parseInt(fList.get(0).get("myCount").toString()) / pageInfo.getPageSize()));
				for(Map<String, Object> fListOne:fList){
					String fNameEn=(String) fListOne.get("fNameEn");
					//如果切换成英文，则显示英文的面料名
					if(language!=null&&language.equals("en")){
						if(fNameEn!=null&&fNameEn!=""){
							fListOne.put("fName", fNameEn);
						}else{
							fListOne.put("fName", "No");
						}
					}
					
				}
			}
			//筛选条件面板语言切换
			if(language.equals("en")){
				for(FabricFilter ffListOne:ffList){
					ffListOne.setFfName(ffListOne.getFfNameEn());
				}
			}
			model.addAttribute("member", member);
			model.addAttribute("type", type);
			model.addAttribute("sId", sId);
			model.addAttribute("query", query);
			model.addAttribute("list", fList);
			model.addAttribute("ffList", ffList);
			model.addAttribute("page", pageInfo);
		}
		return "web/fabric/fabricCatalogue";
	}

	/**
	 * 
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月6日 上午10:26:32
	 * @version 1.0
	 * @see 面料详情
	 * @param
	 * @parameter type 类别fId 面料id
	 * @since
	 * @return
	 */
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	public String getDetailById(Integer fId, Model model) {
		String language = languageService.getCurrentLanguage();
		if (fId != null) {
			// 面料详情
			Fabric fb = fabricService.getDetailById(fId);
			if(language!=null&&language.equals("en")){
				//将面料名和地区名国际化
				fb.setfName(fb.getfMessage1());
				fb.setfOrigin(fb.getfMessage2());
				fb.setfMessage3(fb.getfMessage4());
			}
			// 面料图片
			List<FabricPic> picList = fabricPicService.getListByFid(fId);
			//面料颜色图片
			List<FabricMorepic> morePicList = fabricMorepicService.findByFabricId(fId);
			// 记录浏览记录
			Member member = memberService.getCurrent();
			if (member != null) {
				MemberAttention mtt = new MemberAttention();
				mtt.setAttid(fId);
				mtt.setAtttype(3);
				mtt.setCreatetime(new Date());
				mtt.setIsdelete(0);
				mtt.setMid(member.getmId());
				memberAttentionService.insert(mtt);
			}
			model.addAttribute("fabric", fb);
			model.addAttribute("picList", picList);
			model.addAttribute("colorPic", morePicList);
		}
		return "web/fabric/liningDetails";
	}

}
 